package com.rzl.expense.expense.service.twr.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.expense.domain.twr.TwrHighTowerSummary;
import com.rzl.expense.expense.mapper.twr.TwrHighTowerSummaryMapper;
import com.rzl.expense.expense.service.twr.ITwrHighTowerSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 铁塔-高价塔占比分析汇总Service业务层处理
 *
 * @author 董涛
 * @date 2024-11-19
 */
@Service
public class TwrHighTowerSummaryServiceImpl implements ITwrHighTowerSummaryService {
    @Autowired
    private TwrHighTowerSummaryMapper twrHighTowerSummaryMapper;


    /**
     * 查询铁塔-高价塔占比分析汇总列表
     *
     * @param twrHighTowerSummary 铁塔-高价塔占比分析汇总
     * @return 铁塔-高价塔占比分析汇总
     */
    @Override
    public List<TwrHighTowerSummary> selectTwrHighTowerSummaryList(TwrHighTowerSummary twrHighTowerSummary) {
        List<TwrHighTowerSummary> list = twrHighTowerSummaryMapper.selectTwrHighTowerSummaryList(twrHighTowerSummary);

        list.forEach(towerSummary -> {

            BigDecimal allCount = towerSummary.getAllCount();
            BigDecimal highTowerCount = towerSummary.getHighTowerCount();
            BigDecimal highTowerNewCount = towerSummary.getHighTowerNewCount();
            BigDecimal a = BigDecimal.ZERO;
            BigDecimal b = BigDecimal.ZERO;
            if (allCount != null && allCount.compareTo(BigDecimal.ZERO) > 0) {
                if (highTowerCount != null) {
                    a = highTowerCount.divide(allCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighTowerRatio(a);
                }
                if (highTowerNewCount != null) {
                    b = highTowerNewCount.divide(allCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighTowerNewRatio(b);
                }
            } else {
                towerSummary.setHighTowerRatio(a);
                towerSummary.setHighTowerNewRatio(b);
            }


            BigDecimal compareAllCount = towerSummary.getCompareAllCount();
            BigDecimal compareHighTowerCount = towerSummary.getCompareHighTowerCount();
            BigDecimal compareHighTowerNewCount = towerSummary.getCompareHighTowerNewCount();


            BigDecimal c = BigDecimal.ZERO;
            BigDecimal d = BigDecimal.ZERO;
            if (compareAllCount != null && compareAllCount.compareTo(BigDecimal.ZERO) > 0) {
                if (compareHighTowerCount != null) {
                    c = compareHighTowerCount.divide(compareAllCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighTowerRate(a.subtract(c));
                }
                if (compareHighTowerNewCount != null) {
                    d = compareHighTowerNewCount.divide(compareAllCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighTowerNewRate(b.subtract(d));
                }
            } else {
                towerSummary.setHighTowerRate(c);
                towerSummary.setHighTowerNewRate(d);
            }

        });
        return list;
    }

    /**
     * 新增铁塔-高价塔占比分析汇总
     *
     * @param twrHighTowerSummary 铁塔-高价塔占比分析汇总
     * @return 结果
     */
    @Override
    public int insertTwrHighTowerSummary(TwrHighTowerSummary twrHighTowerSummary) {
        twrHighTowerSummary.setCreateTime(DateUtils.getNowDate());
        return twrHighTowerSummaryMapper.insertTwrHighTowerSummary(twrHighTowerSummary);
    }


    /**
     * 删除铁塔-高价塔占比分析汇总信息
     *
     * @param staDate 铁塔-高价塔占比分析汇总主键
     * @return 结果
     */
    @Override
    public int deleteTwrHighTowerSummaryByStaDate(String staDate) {
        return twrHighTowerSummaryMapper.deleteTwrHighTowerSummaryByStaDate(staDate);
    }

    @Override
    public void batchTwrHighTowerSummary(List<TwrHighTowerSummary> list) {
        twrHighTowerSummaryMapper.batchTwrHighTowerSummary(list);
    }

    @Override
    public List<TwrHighTowerSummary> summaryHighTowerByStaDate(String staDate) {
        return twrHighTowerSummaryMapper.summaryHighTowerByStaDate(staDate);
    }

    @Override
    public List<String> summaryStaDates() {
        return twrHighTowerSummaryMapper.summaryStaDates();
    }

    @Override
    public TwrHighTowerSummary summaryCompareHighTower(TwrHighTowerSummary summary) {
        return twrHighTowerSummaryMapper.summaryCompareHighTower(summary);
    }

    @Override
    public List<TwrHighTowerSummary> allYearTrend(TwrHighTowerSummary summary) {
        List<TwrHighTowerSummary> list = twrHighTowerSummaryMapper.allYearTrend(summary);
        list.forEach(towerSummary -> {
            BigDecimal allCount = towerSummary.getAllCount();
            BigDecimal highTowerCount = towerSummary.getHighTowerCount();
            BigDecimal highTowerNewCount = towerSummary.getHighTowerNewCount();
            BigDecimal a = BigDecimal.ZERO;
            BigDecimal b = BigDecimal.ZERO;
            if (allCount != null && allCount.compareTo(BigDecimal.ZERO) > 0) {
                if (highTowerCount != null) {
                    a = highTowerCount.divide(allCount, 2, BigDecimal.ROUND_UP);
                    towerSummary.setHighTowerRatio(a);
                }
                if (highTowerNewCount != null) {
                    b = highTowerNewCount.divide(allCount, 2, BigDecimal.ROUND_UP);
                    towerSummary.setHighTowerNewRatio(b);
                }
            } else {
                towerSummary.setHighTowerRatio(a);
                towerSummary.setHighTowerNewRatio(b);
            }

        });
        return list;
    }

}
